#include "OrientationComponent.h"
#include "TransformationNode.h"
#include "Trigger.h"

const HashedString OrientationComponent::type("OrientationComponent");

OrientationComponent::OrientationComponent( Entity* parent ) : IComponent(parent), m_transformNode(new TransformationNode(&m_position, &m_rotation))
{
}

OrientationComponent::OrientationComponent( OrientationComponent& o ) : IComponent(o)
{
	m_position = o.m_position;
	m_rotation = o.m_rotation;
}

OrientationComponent::~OrientationComponent()
{
}

const HashedString& OrientationComponent::Type()
{
	return type;
}

Subject<Maths::Vector3D>& OrientationComponent::getPosition()
{
	return m_position;
}
Subject<Maths::Quaternion>& OrientationComponent::getRotation()
{
	return m_rotation;
}

Pointer<INode>& OrientationComponent::getTransformNode()
{
	return m_transformNode;
}

void OrientationComponent::Move( Maths::Vector3D& movement )
{
	m_position += movement;
};

void OrientationComponent::Rotate( Maths::Quaternion& rotation )
{
	m_rotation = (Maths::Quaternion)m_rotation * rotation;
}

std::string OrientationComponent::ToString()const
{
	std::string string;
	Maths::Vector3D position = (Maths::Vector3D&)m_position;
	Maths::Quaternion rotation((Maths::Quaternion&)m_rotation);
	Maths::Vector3D euler;
	rotation.ToEuler(euler);
	
	string += "Position=";
	
	char buffer[50];
	sprintf_s(buffer, 50, "%.3f, %.3f, %.3f", position.X(), position.Y(), position.Z());

	string += buffer;
	
	string += " Rotation=";

	sprintf_s(buffer, 50, "%.3f, %.3f, %.3f", euler.X(), euler.Y(), euler.Z());
	
	string += buffer;

	return string;
}
bool OrientationComponent::ToXML( tinyxml2::XMLPrinter& printer )
{
	std::string string;
	Maths::Vector3D position = m_position.getObject();
	Maths::Quaternion rotation = m_rotation.getObject();
	Maths::Vector3D euler;
	rotation.ToEuler(euler);
	
	char buffer[50];
	sprintf_s(buffer, 50, "%.3f %.3f %.3f", position.X(), position.Y(), position.Z());

	printer.OpenElement("Position");
	printer.PushAttribute("Vector", buffer);
	printer.CloseElement();
	
	sprintf_s(buffer, 50, "%.3f %.3f %.3f", euler.X(), euler.Y(), euler.Z());

	printer.OpenElement("Rotation");
	printer.PushAttribute("Vector", buffer);
	printer.CloseElement();

	return true;
}